package com.ocom.webapi.controller;

import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.entity.noticeBoard.EntityNoticeBoardInfo;
import com.ocom.common.request.ComIdRequest;
import com.ocom.common.request.IdRequest;
import com.ocom.common.request.company.GetNoticeBoardByComRequest;
import com.ocom.common.request.company.GetNoticeBoardListRequest;
import com.ocom.common.resp.Result;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.CompanyClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@PreAuthorize("@pms.hasRole('ROLE_WEB')")
@Slf4j
@RestController
public class NoticeBoardController {

    @Autowired
    CompanyClientService companyClientService;

    @PostMapping(WebApiServiceUrl.URL_noticeBoardOnShow)
    public Result noticeBoardOnShow(@RequestBody GetNoticeBoardListRequest request) {
        return companyClientService.getNoticeBoardList(request);
    }

    @PostMapping(WebApiServiceUrl.URL_noticeBoardAdd)
    public Result noticeBoardAdd(@RequestBody EntityNoticeBoardInfo request) {
        return companyClientService.addNoticeBoard(request);
    }

    @PostMapping(WebApiServiceUrl.URL_noticeBoardUpdate)
    public Result noticeBoardUpdate(@RequestBody EntityNoticeBoardInfo request) {
        return companyClientService.updateNoticeBoard(request);
    }

    @PostMapping(WebApiServiceUrl.URL_noticeBoardDelete)
    public Result noticeBoardDelete(@RequestBody IdRequest idRequest) {
        return companyClientService.deleteNoticeBoard(idRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_homeNoticeOnShow)
    public Result homeNoticeOnShow(){
        Map<String, Object> tokenParse = SecurityUtils.getTokenParse();
        Integer comId = (Integer) tokenParse.get("comId");
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        GetNoticeBoardByComRequest request = new GetNoticeBoardByComRequest();
        request.setRole(yoCiUser.getRole());
        request.setComId(yoCiUser.getComId());
        return companyClientService.homeNoticeOnShow( request);
    }
}
